/**
 * {
 *  name: {
 *   defaultValue: 'xxx',
 *   rules: []
 *  },
 *  age: {
 *   defaultValue: 18,
 *   rules: []
 *  }
 * }
 */

export default <T extends object>(form: {
  [key in keyof T]: {
    defaultValue: T[key]
    rules?: any[]
  }
}) => {
  const formData = ref(
    Object.keys(form).reduce((acc, key) => {
      acc[key] = form[key].defaultValue
      return acc
    }, {} as T)
  )
  const formRules = ref(
    Object.keys(form).reduce((acc, key) => {
      form[key].rules && (acc[key] = form[key].rules)
      return acc
    }, {} as T)
  )

  const resetFormData = () => {
    Object.keys(form).forEach((key) => {
      formData.value[key] = form[key].defaultValue
    })
  }

  return {
    formData,
    formRules,
    resetFormData
  }
}
